package cn.tedu.servlet;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * Author：程Sir
 * Name： SmallServer
 * Content:
 */
public class RandomAccessFileDemo {

    public static void main(String[] args) {
        try(RandomAccessFile randomAccessFile = new RandomAccessFile(new File("user.txt"),"rw")) {

            randomAccessFile.writeInt(20);
            System.out.println("指针的位置："+randomAccessFile.getFilePointer()); // 4

            randomAccessFile.write("A".getBytes("utf-8"));
            System.out.println("指针的位置："+randomAccessFile.getFilePointer()); // 5
            System.out.println("================");

            //将指针回退到开始位置
            randomAccessFile.seek(0);
            int i = randomAccessFile.readInt();
            System.out.println("读到的内容："+i);
            System.out.println("指针位置："+randomAccessFile.getFilePointer());
            System.out.println("读到的内容："+(char)randomAccessFile.read());
            System.out.println("================");
            //将指针回退到开始位置
            randomAccessFile.seek(0);
            int length=randomAccessFile.read(new byte[1025]);
            System.out.println(length);


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }





}
